package com.sykj.huxianlong.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sykj.huxianlong.entity.SysConstant;
import com.sykj.huxianlong.entity.User;
import com.sykj.huxianlong.mapper.UserMapper;
import com.sykj.huxianlong.service.ICacheService;
import com.sykj.huxianlong.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.servlet.http.Cookie;


import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sunt
 * @since 2020-05-18
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ICacheService cacheService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    public User getCurrentUser(String userkey) {
        redisTemplate.boundValueOps(userkey).get();

        Cookie cookie = ServletUtil.getCookie(request, SysConstant.ACTIVITI_COOKIE.toLowerCase());
//        if(ObjectUtil.isNull(cookie)) {
//            return null;
//        }
//        String loginToken = cookie.getValue();
//        User cacheUser = (User) cacheService.getObjCacheByCode(loginToken);
//        if(ObjectUtil.isNull(cacheUser)) {
//            return null;
//        }
        return (User) redisTemplate.boundValueOps(userkey).get();
    }



    @Override
    public User queryUserById(String userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_ID",userId);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User doLogin(String userName, String passWord) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_NAME",userName);
        User user = userMapper.selectOne(queryWrapper);
//        if(ObjectUtil.isNull(user)) {
//            return ResponseUtil.makeErrRsp(ResultCode.FAIL.code,"用户不存在");
//        }
//        if(!StrUtil.equals(user.getUserPass(), SecureUtil.md5(passWord))) {
//            return ResponseUtil.makeErrRsp(ResultCode.FAIL.code,"用户名或密码错误");
//        }
        return user;
    }

}
